/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 * xử lý chỗ vẽ bài của từng player
 * Xử lý nếu sau khi chuyển bài - Xếp lại bài (chuồn - rô - bích - cơ)
 * Xử lý mouse moved đến 1 lá bài
 * Xử lý up cho các lá bài của đối thủ
 * Xử lý đánh bài
 */
/*
 * DrawCard.java
 *
 * Created on May 31, 2011, 11:57:46 AM
 */
package Presentation;

import Game.GlobalSettings;
import Game.MyCard;
import Game.MyGame;
import MyThread.ThreadprocessGame;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
//import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
//import java.util.Timer;
//import java.util.TimerTask;
import javax.swing.Timer;
//import javax.swing.JFrame;
//import javax.swing.JOptionPane;


/**
 *
 * @author
 */
public class DrawCard extends javax.swing.JFrame implements MouseMotionListener,MouseListener {

    private MyGame game;
    MyCard card;
    int curX, curY;

    Timer timer;


    //Thread threaGame = new Thread();

    /** Creates new form DrawCard */
    public DrawCard() {
        initComponents();
        this.setResizable(false);
        this.setLocation(80, 20);
        this.setSize(GlobalSettings.ScreenWidth, GlobalSettings.ScreenHeight);

        //this.InitDrawCard();
    }

    public void InitDrawCard() {
        addMouseMotionListener(this);
        addMouseListener(this);

        ActionListener listener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                update(null);
            }
        };

        timer = new Timer(10, listener);
        timer.start();
        game = new MyGame();
        ThreadprocessGame threadGame = new ThreadprocessGame(game);
        //game.startGame();
//
//        card = new MyCard(5, 3, beginPos);
//        card.changeBackCardImage();
        repaint();
    }

    @Override
    public void update(Graphics g) {
        if(!game.isIsFinishedMakeDeal())
        {
            game.Update();
            game.makeDealFinished();
        }
        repaint();
    }

    @Override
    public void paint(Graphics g) {
        Graphics offgc;
        Image offscreen = null;
        Dimension d = this.getSize();

        // create the offscreen buffer and associated Graphics
        offscreen = createImage(d.width, d.height);
        offgc = offscreen.getGraphics();

        // clear the exposed area
        offgc.setColor(this.getBackground());
        offgc.fillRect(0, 0, d.width, d.height);
        offgc.setColor(this.getForeground());

        // do normal redraw
        if (game != null) {
            game.Draw(offgc);
        }
        g.drawImage(offscreen, 0, 0, this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_formMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DrawCard dc = new DrawCard();
                dc.setVisible(true);
                dc.InitDrawCard();
            }
        });
    }

    public void mouseDragged(MouseEvent e) {

    }

    public void mouseMoved(MouseEvent e) {
        if (!game.isFinishRound())
        {
            curX = e.getX();
            curY = e.getY();
            if(game.isIsFinishedMakeDeal())
                game.processCursor(curX, curY);
            repaint();
        }
    }

    public void mouseClicked(MouseEvent e) {
        //JOptionPane.showMessageDialog(null, "123");
        //game.processCardSend();
        
        //repaint();
    }

    public void mousePressed(MouseEvent e) {
       if(game.isPlayerHasTurn(0) && game.processCardSend())
       {           
           game.setbPlayerSend(true);
           game.setPlayerHasTurn(0,false);
        }
    }

    public void mouseReleased(MouseEvent e) {
        //repaint();
    }

    public void mouseEntered(MouseEvent e) {
        //repaint();
    }

    public void mouseExited(MouseEvent e) {
        //repaint();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
